package engine.display.newAttack.action.effect
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Linear;
	
	import engine.logic.McPool;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;
	
	public class PointToPointLite
	{
		/**特效容器，用来显示特效*/
		private var space				:MovieClip	=	null;
		
		/**特效资源*/
		private var _skin				:MovieClip;
		
		/**产生点*/
		private var _birthPoint			:Point;
		
		/**消失点*/		
		private var _deathPoint			:Point;
				
		/**运行速度,单位：像素/秒*/		
		private var _speed				:int;
		
		/**播放过程中触发*/
		private var _onPlay				:Function	=	null;
		
		/**播放完成后播放*/
		private var _onPlayOver			:Function	=	null;
		
		//==============================================================
				
		/**曲线比：即弧度高与距离的比*/		
		private var _curveVal			:Number		=	0;
						
		public function PointToPointLite(effectSpace	:MovieClip,
										effectSkin		:MovieClip,
										birthPoint		:Point,
										deathPoint		:Point,
										speed			:int,
										curveVal		:Number = 0,
										onPlay			:Function	=	null,
										onPlayOver		:Function	=	null)
		{
			
			if(null	==	effectSkin)
			{//当没特效动画的时候，不播放特效
				return;
			}	
			
			space			=	effectSpace;
			_skin			=	effectSkin;
			_birthPoint		=	birthPoint;
			_deathPoint		=	deathPoint;			
			_speed			=	speed;
			
			_curveVal		=	curveVal;
			_onPlay			=	onPlay;
			_onPlayOver		=	onPlayOver;
			
			
			init()
		}
		
		private function init():void
		{
			_skin.addEventListener(Event.ADDED_TO_STAGE,onHeadAdd);
			space.addChild(_skin);
		}
		
		private function onHeadAdd(e:Event):void
		{
			_skin.removeEventListener(Event.ADDED_TO_STAGE,onHeadAdd);
			//生成特效
			createEffect();
		}
		
		/**
		 * 生成特效
		 */		
		private function createEffect():void
		{
			//定位发射点
			_skin.x		=	_birthPoint.x;
			_skin.y		=	_birthPoint.y;
			
			//计算距离
			var distance	:int	=	int(Point.distance(_birthPoint,_deathPoint));
			
			//计算时间
			var time		:Number	= 	int(1000*distance/_speed)*0.001;
						
			//计算曲线高
			var h			:Number	=	distance*_curveVal;
			
			var centerX:Number		=	(_birthPoint.x+_deathPoint.x)*.5
			var centerY:Number		=	(_birthPoint.y+_deathPoint.y)*.5
		
			var angle		:Number = Math.atan(-(_birthPoint.y-_deathPoint.y)/(_birthPoint.x-_deathPoint.x));
			var realangle	:Number = Math.PI / 2.0 - angle;
			
			var tagetX		:int = int(centerX - h * Math.cos(realangle));
			var tagetY		:int = int(centerY - h * Math.sin(realangle));
			
			//开始生成轨迹
			TweenMax.to(_skin, time, {bezier:[{x:tagetX, y:tagetY}, {x:_deathPoint.x, y:_deathPoint.y}], orientToBezier:true, ease:Linear.easeNone,onUpdate:onEffectPlay,onComplete:onEffectPlayerOver});
		}
		
		private function onEffectPlay():void
		{
			if(_onPlay!=null)
			{
				_onPlay.call(this,this);
			}
		}
		
		/**
		 * 当弹道到达终点时
		 */		
		private function onEffectPlayerOver():void
		{	
			clearEffect()
		}
		
		/**
		 * 特效播放结束 
		 * 
		 */		
		private function clearEffect():void
		{
			if(_onPlayOver!=null)
			{			
				_onPlayOver.call(this,this);
			}
			
			if(_skin != null)
			{
				//关闭所有特效，删除所有粒子
				TweenMax.killChildTweensOf(_skin,true);
				
				if(_skin.parent != null)
				{
					_skin.parent.removeChild(_skin);
				}
				
				McPool.returnRes( _skin["url"], _skin );
			}
			
			_birthPoint		=	null;
			_deathPoint		=	null;
			_skin			= 	null;
			_onPlay			=	null;
			_onPlayOver		=	null;
			space			=	null;
						
//			GC.gc();
		}

	}
}